<include file="public@header_vue"/>
<style>
    .el-pagination {
        margin-top: 20px;
    }

    .avatar {
        width: 40px;
        height: auto;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .demo-table-expand {
        font-size: 0;
    }

    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }

    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 100%;
    }

</style>
</head>
<body>

<div id="app">
    <el-tabs type="border-card">
        <el-tab-pane label="沟通记录管理" v-loading="onDataLoading">
            <el-row>
                <el-col :span="24">
                    <el-form :inline="true" :model="form" size="mini">
                        <el-form-item label="日期:">
                            <el-date-picker v-model="form.duration" type="daterange" value-format="yyyy-MM-dd"
                                            :picker-options="durationOptions" range-separator="至"
                                            start-placeholder="开始日期" end-placeholder="结束日期" clearable></el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="getData()">查询</el-button>
                            <el-button type="danger" @click="clearForm()">清空</el-button>

                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column type="expand">
                    <template slot-scope="props">
                        <el-form label-position="left" inline class="demo-table-expand">
                            <el-form-item label="添加地址">
                                <span>{{ props.row.address }}</span>
                            </el-form-item>
                            <el-form-item label="凭证">
                                <el-image v-for="img in props.row.img_url"
                                          style="width: 100px;"
                                          :src="img"
                                          lazy
                                          :preview-src-list="props.row.img_url">
                                </el-image>
                            </el-form-item>
                            <el-form-item v-if="props.row.is_finished == 1" label="型号">
                                <span>{{ props.row.model_no }}</span>
                            </el-form-item>
                            <el-form-item v-if="props.row.is_finished == 1" label="价格">
                                <span>{{ props.row.order_price }}</span>
                            </el-form-item>
                            <el-form-item v-if="props.row.is_finished == 1" label="订单号">
                                <span>{{ props.row.order_sn }}</span>
                            </el-form-item>
                            <el-form-item v-if="props.row.is_finished == 1" label="订单凭证">
                                <el-image v-for="img in props.row.order_pic"
                                          style="width: 100px;"
                                          :src="img"
                                          lazy
                                          :preview-src-list="props.row.order_pic">
                                </el-image>
                            </el-form-item>
                        </el-form>
                    </template>
                </el-table-column>
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column prop="admin_name" label="操作人"></el-table-column>
                <el-table-column prop="admin_role_name" label="角色"></el-table-column>
                <el-table-column v-if="{$role_id}!=3" prop="company_name" label="公司名称"></el-table-column>
                <el-table-column prop="customer_info.true_name" label="客户姓名"></el-table-column>
                <el-table-column prop="customer_status_name" label="客户状态"></el-table-column>
                <el-table-column prop="progress_status_name" label="进度状态"></el-table-column>
                <el-table-column label="进度标签">
                    <template slot-scope="scope">
                        <el-tag v-for=" (tag,index) in scope.row.tag_ids" :key="index">{{tag}}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="审核状态"></el-table-column>
                <el-table-column prop="create_time" label="创建时间" min-width="120">
                    <template slot-scope="scope">
                        {{formatDate(scope.row.create_time)}}
                    </template>
                </el-table-column>
                <el-table-column prop="update_time" label="更新时间" min-width="120">
                    <template slot-scope="scope">
                        {{formatDate(scope.row.update_time)}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="180">
                    <template slot-scope="scope">
                        <!--                        <el-button size="mini" type="text" @click="update(scope.row)">查看</el-button>-->
                        <!--                        <el-button size="mini" type="text" @click="delete_source(scope.row.id)">删除</el-button>-->
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination background layout="total,prev, pager, next" :total="totalNum" :page-size="10"
                           :current-page.sync="currentPage" @current-change="getData"></el-pagination>
        </el-tab-pane>
        <el-dialog :title="sourceFrom.id == '' ? '新增客户来源' : '查看详细'" :visible.sync="dialogVisible" width="60%"
                   v-loading.fullscreen="dialogLoading">
            <el-form :model="sourceFrom" class="demo-form-inline" :rules="rules" ref="sourceFrom"
                     label-width="200px">
                <el-form-item label="客户来源" prop="name">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.name" placeholder="客户来源名称"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item v-if="{$role_id}!=3" label="选择公司" prop="company_id">
                    <el-col :span="18">
                        <el-select v-model="sourceFrom.company_id" placeholder="选择公司">
                            <el-option
                                    v-for="item in companyData"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-form-item>
                <el-form-item label="扩展字段" prop="extend_field">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.extend_field" placeholder="扩展字段"></el-input>
                    </el-col>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button v-if="sourceFrom.id === ''" type="primary" @click="submit('sourceFrom')">新增</el-button>
            <el-button v-else type="primary" @click="submit('sourceFrom')">修改</el-button>
        </span>
        </el-dialog>
    </el-tabs>
</div>
</body>
<script>
    let id = 1000;
    var app = new Vue({
        el: '#app',
        data: function () {
            var checkPhone = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('手机号不能为空'));
                } else {
                    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的手机号'));
                    }
                }
            };
            let self = this;
            return {
                tableData: [],
                companyData: [],
                form: {
                    duration: '',
                },
                sourceFrom: {
                    id: '',
                    name: '',
                    extend_field: '',
                },
                durationOptions: {
                    shortcuts: [{
                        text: '一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                dialogVisible: false,
                dialogLoading: true,
                totalNum: 0,
                currentPage: 1,
                parent_info: [],
                show_name: {
                    label: 'name'
                },
                onDataLoading: false,
                headers: {'X-Requested-With': 'XMLHttpRequest'},
                rules: {
                    name: [{required: true, message: '请输入客户来源名称', trigger: 'blur'}],
                    company_id: [{required: true, message: '请选择公司', trigger: 'blur'}],
                },
            };

        },
        created() {
            this.getData()
            this.getCompanyData()
        },
        methods: {
            clearForm() {
                this.form = {
                    name: '',
                }
            },
            getCompanyData() {
                let _this = this;
                axios.get("{:url('admin/company/get_company_list')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                }).then(function (response) {
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.companyData = response.data.data;
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            update(data) {
                let _this = this;
                console.log(data)
                if (data) {
                    _this.sourceFrom = JSON.parse(JSON.stringify(data));
                } else {
                    _this.sourceFrom = {
                        id: '',
                        name: '',
                        extend_field: '',
                    }
                }
                _this.dialogVisible = true;
                _this.dialogLoading = false;
            },

            submit(form) {
                let _this = this;
                _this.$refs[form].validate((valid) => {
                    if (valid) {
                        axios.post("{:url('admin/customer_source/update')}", this.sourceFrom, {
                            headers: {
                                'X-Requested-With': 'XMLHttpRequest',
                            }
                        }).then(function (response) {
                            _this.dialogLoading = false;
                            if (response.data.code === 1) {
                                if (_this.sourceFrom.id === '') {
                                    _this.$message.success('新增完成');
                                } else {
                                    _this.$message.success('修改成功');
                                }
                                _this.getData();
                                _this.dialogVisible = false;
                            } else {
                                _this.$message.warning(response.data.msg);
                            }
                        }).catch(function (error) {
                            _this.dialogLoading = false;
                            _this.$message.error(error.toString())
                        })
                    } else {
                        _this.dialogLoading = false;
                        return false;
                    }
                });
            },
            remove(node, data) {
                let _this = this;
                const parent = node.parent;
                const children = parent.data.children || parent.data;
                const index = children.findIndex(d => d.id === data.id);
                console.log(children[index].children)
                if (children[index].children == undefined || children[index].children.length == 0) {
                    children.splice(index, 1);
                } else {
                    _this.$message.error('该角色下还有未删除的角色');
                }
            },

            //首页获取列表数据
            getData(page = 1) {
                let _this = this;
                let params = {page: page};
                this.onDataLoading = true;
                if (this.form.duration !== '') {
                    params.duration = this.form.duration;
                }
                console.log(params);
                axios.get("{:url('admin/connect_log/logs')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                    params: params
                }).then(function (response) {
                    console.log(response.data.data);
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.tableData = response.data.data.data;
                        _this.totalNum = response.data.data.total;
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },

            change_status(field, value, id) {
                let _this = this;
                this.dialogLoading = true;
                params = {field: field, value: value, id: id};
                axios.post("{:url('admin/customer_source/change_status')}", params, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    }
                }).then(function (response) {
                    _this.dialogLoading = false;
                    if (response.data.code === 1) {
                        _this.$message.success('修改完成');
                        _this.getData(_this.currentPage);
                    } else {
                        _this.$message.warning(response.data.msg);
                    }
                })
            },
            delete_source(id) {
                let _this = this;
                _this.dialogLoading = true;
                if (id) {
                    params = {id: id};
                    axios.post('/admin/customer_source/delete', params, {
                        headers: {
                            'X-Requested-With': 'XMLHttpRequest',
                        }
                    }).then(function (response) {
                        _this.dialogLoading = false;
                        if (response.data.code === 1) {
                            _this.$message.success('删除完成');
                            _this.getData(_this.currentPage);
                        } else {
                            _this.$message.warning(response.data.msg);
                        }
                    }).catch(function (error) {
                        _this.dialogLoading = false;
                        _this.$message.error(error.toString())
                    })
                }
            },
            formatDate(timestamp) {
                let date = new Date(timestamp * 1000);
                return date.getFullYear() + '-' + this.prefix(date.getMonth() + 1) + '-' + this.prefix(date.getDate()) + ' ' + this.prefix(date.getHours()) + ':' + this.prefix(date.getMinutes()) + ':' + this.prefix(date.getSeconds());
            },
            prefix(num) {
                if (num < 10) {
                    return '0' + num;
                }
                return num;
            }
        }
    });
</script>
